package fun.boomcat.luckyhe.mirai.plugin.luckymoney;

import fun.boomcat.luckyhe.mirai.plugin.luckymoney.command.MoneyCommand;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.command.OpMoneyCommand;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.util.ConfigUtil;
import net.mamoe.mirai.console.command.CommandManager;
import net.mamoe.mirai.console.extension.PluginComponentStorage;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.console.permission.PermissionId;
import net.mamoe.mirai.console.permission.PermissionRegistryConflictException;
import net.mamoe.mirai.console.permission.PermissionService;
import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Lucky_He
 * @date 2021年9月5日 14:11
 * @description LuckyMoney
 */
public class LuckyMoney extends JavaPlugin {

    public static LuckyMoney INSTANCE = new LuckyMoney();

    private Permission moneyPerm;
    private Permission opMoneyPerm;

    private LuckyMoney() {
        super(new JvmPluginDescriptionBuilder(
                "luckyhe.luckymoney",
                "1.0"
        ).build());
    }

    @Override
    public void onLoad(@NotNull PluginComponentStorage $this$onLoad) {
        try {
            ConfigUtil.initConfigPath(getConfigFolder(), getResource("config.properties", StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onEnable() {
        INSTANCE.getLogger().info("开始加载插件");
        loadPermissions();
        INSTANCE.getLogger().info("注册权限完成");
        loadCommands();
        INSTANCE.getLogger().info("注册指令完成");
    }

    private void loadCommands() {
        String commandPrefix = CommandManager.INSTANCE.getCommandPrefix();

        String[] moneySecondaryNames = {"bal", "balance", "钱", "余额", "金钱", "钱包"};
        CommandManager.INSTANCE.registerCommand(new MoneyCommand(
                this,
                "money",
                moneySecondaryNames,
                commandPrefix + "money <操作>",
                "money相关指令",
                moneyPerm,
                false
        ), false);

        String[] opMoneySecondaryNames = {"管理钱"};
        CommandManager.INSTANCE.registerCommand(new OpMoneyCommand(
                this,
                "opmoney",
                opMoneySecondaryNames,
                commandPrefix + "opmoney <指令>",
                "opmoney相关指令",
                opMoneyPerm,
                false
        ), false);
    }

    private void loadPermissions() {
        PermissionId moneyPermId = new PermissionId("luckymoney", "command.money");
        PermissionId opMoneyPermId = new PermissionId("luckymoney", "command.opmoney");
        try {
            moneyPerm = PermissionService.getInstance().register(
                    moneyPermId,
                    "money相关指令",
                    Permission.getRootPermission()
            );
            opMoneyPerm = PermissionService.getInstance().register(
                    opMoneyPermId,
                    "opmoney相关指令",
                    Permission.getRootPermission()
            );
        } catch (PermissionRegistryConflictException e) {
            e.printStackTrace();
        }
    }


}
